<?php
/**
 * Created by PhpStorm.
 * User: pf
 * Date: 2017/12/6
 * Time: 14:00
 */

namespace App\Service;


use GuzzleHttp\Client;

class WechatPageService
{
    protected $app;
    protected $http;
    protected $url = 'https://api.weixin.qq.com/shakearound/page/';

    public function __construct()
    {
        $this->app = app('wechat');
        $this->http = new Client();
    }

    protected function getAccessToken()
    {
        //得到access_token实例
        $accessToken = $this->app->access_token;
        return $accessToken->getToken();
    }

    /**
     * @param $title
     * @param $description
     * @param $page_url
     * @param $icon_url
     * @param string $comment
     * @return mixed
     * 增加页面
     */
    public function add($title,$description,$page_url,$icon_url,$comment='')
    {
        $url = $this->url . 'add?access_token=' . $this->getAccessToken();
        $data=compact('title','description','page_url','icon_url','comment');
        $response = $this->http->post(
            $url, ['body' => json_encode($data)]
        );
        return json_decode((string)$response->getBody(), true);
    }

    /**
     * @param $page_id
     * @param $title
     * @param $description
     * @param $page_url
     * @param $icon_url
     * @param string $comment
     * @return mixed
     */
    public function update($page_id,$title,$description,$page_url,$icon_url,$comment=''){
        $url = $this->url . 'update?access_token=' . $this->getAccessToken();
        $data=compact('page_id','title','description','page_url','comment');
        $response = $this->http->post(
            $url, ['body' => json_encode($data)]
        );
        return json_decode((string)$response->getBody(), true);
    }
    //查询页面
    /**
     * @param array $data
     * @return mixed
     */
    public function search(array $data){
        $url = $this->url . 'search?access_token=' . $this->getAccessToken();
        $response = $this->http->post(
            $url, ['body' => json_encode($data)]
        );
        return json_decode((string)$response->getBody(), true);
    }
    public function delete($page_id){
        $url = $this->url . 'search?access_token=' . $this->getAccessToken();
        $data=compact('page_id');
        $response = $this->http->post(
            $url, ['body' => json_encode($data)]
        );
        return json_decode((string)$response->getBody(), true);
    }
}